<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Qt 4.3: basictoolsplugin.cpp Example File (tools/plugandpaintplugins/basictools/basictoolsplugin.cpp)</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">basictoolsplugin.cpp Example File<br /><sup><sup>tools/plugandpaintplugins/basictools/basictoolsplugin.cpp</sup></sup></h1>
<pre><span class="comment"> /****************************************************************************
 **
 ** Copyright (C) 2005-2008 Trolltech ASA. All rights reserved.
 **
 ** This file is part of the documentation of the Qt Toolkit.
 **
 ** This file may be used under the terms of the GNU General Public
** License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file.  Alternatively you may (at
** your option) use any later version of the GNU General Public
** License if such license has been publicly approved by Trolltech ASA
** (or its successors, if any) and the KDE Free Qt Foundation. 
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/. If
** you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** In addition, as a special exception, Trolltech, as the sole
** copyright holder for Qt Designer, grants users of the Qt/Eclipse
** Integration plug-in the right for the Qt/Eclipse Integration to
** link to functionality provided by Qt Designer and its related
** libraries.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not expressly
** granted herein.
 **
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 **
 ****************************************************************************/</span>

 #include &lt;QtGui&gt;

 #include &lt;math.h&gt;
 #include &lt;stdlib.h&gt;

 #include &quot;basictoolsplugin.h&quot;

 const float Pi = 3.14159f;

 QStringList BasicToolsPlugin::brushes() const
 {
     return QStringList() &lt;&lt; tr(&quot;Pencil&quot;) &lt;&lt; tr(&quot;Air Brush&quot;)
                          &lt;&lt; tr(&quot;Random Letters&quot;);
 }

 QRect BasicToolsPlugin::mousePress(const QString &amp;brush, QPainter &amp;painter,
                                    const QPoint &amp;pos)
 {
     return mouseMove(brush, painter, pos, pos);
 }

 QRect BasicToolsPlugin::mouseMove(const QString &amp;brush, QPainter &amp;painter,
                                   const QPoint &amp;oldPos, const QPoint &amp;newPos)
 {
     painter.save();

     int rad = painter.pen().width() / 2;
     QRect boundingRect = QRect(oldPos, newPos).normalized()
                                               .adjusted(-rad, -rad, +rad, +rad);
     QColor color = painter.pen().color();
     int thickness = painter.pen().width();
     QColor transparentColor(color.red(), color.green(), color.blue(), 0);

     if (brush == tr(&quot;Pencil&quot;)) {
         painter.drawLine(oldPos, newPos);
     } else if (brush == tr(&quot;Air Brush&quot;)) {
         int numSteps = 2 + (newPos - oldPos).manhattanLength() / 2;

         painter.setBrush(QBrush(color, Qt::Dense6Pattern));
         painter.setPen(Qt::NoPen);

         for (int i = 0; i &lt; numSteps; ++i) {
             int x = oldPos.x() + i * (newPos.x() - oldPos.x()) / (numSteps - 1);
             int y = oldPos.y() + i * (newPos.y() - oldPos.y()) / (numSteps - 1);

             painter.drawEllipse(x - (thickness / 2), y - (thickness / 2),
                                 thickness, thickness);
         }
     } else if (brush == tr(&quot;Random Letters&quot;)) {
         QChar ch('A' + (qrand() % 26));

         QFont biggerFont = painter.font();
         biggerFont.setBold(true);
         biggerFont.setPointSize(biggerFont.pointSize() + thickness);
         painter.setFont(biggerFont);

         painter.drawText(newPos, QString(ch));

         QFontMetrics metrics(painter.font());
         boundingRect = metrics.boundingRect(ch);
         boundingRect.translate(newPos);
         boundingRect.adjust(-10, -10, +10, +10);
     }
     painter.restore();
     return boundingRect;
 }

 QRect BasicToolsPlugin::mouseRelease(const QString &amp; <span class="comment">/* brush */</span>,
                                      QPainter &amp; <span class="comment">/* painter */</span>,
                                      const QPoint &amp; <span class="comment">/* pos */</span>)
 {
     return QRect(0, 0, 0, 0);
 }

 QStringList BasicToolsPlugin::shapes() const
 {
     return QStringList() &lt;&lt; tr(&quot;Circle&quot;) &lt;&lt; tr(&quot;Star&quot;) &lt;&lt; tr(&quot;Text...&quot;);
 }

 QPainterPath BasicToolsPlugin::generateShape(const QString &amp;shape,
                                              QWidget *parent)
 {
     QPainterPath path;

     if (shape == tr(&quot;Circle&quot;)) {
         path.addEllipse(0, 0, 50, 50);
     } else if (shape == tr(&quot;Star&quot;)) {
         path.moveTo(90, 50);
         for (int i = 1; i &lt; 5; ++i) {
             path.lineTo(50 + 40 * cos(0.8 * i * Pi),
                         50 + 40 * sin(0.8 * i * Pi));
         }
         path.closeSubpath();
     } else if (shape == tr(&quot;Text...&quot;)) {
         QString text = QInputDialog::getText(parent, tr(&quot;Text Shape&quot;),
                                              tr(&quot;Enter text:&quot;),
                                              QLineEdit::Normal, tr(&quot;Qt&quot;));
         if (!text.isEmpty()) {
             QFont timesFont(&quot;Times&quot;, 50);
             timesFont.setStyleStrategy(QFont::ForceOutline);
             path.addText(0, 0, timesFont, text);
         }
     }

     return path;
 }

 QStringList BasicToolsPlugin::filters() const
 {
     return QStringList() &lt;&lt; tr(&quot;Invert Pixels&quot;) &lt;&lt; tr(&quot;Swap RGB&quot;)
                          &lt;&lt; tr(&quot;Grayscale&quot;);
 }

 QImage BasicToolsPlugin::filterImage(const QString &amp;filter, const QImage &amp;image,
                                      QWidget * <span class="comment">/* parent */</span>)
 {
     QImage result = image.convertToFormat(QImage::Format_RGB32);

     if (filter == tr(&quot;Invert Pixels&quot;)) {
         result.invertPixels();
     } else if (filter == tr(&quot;Swap RGB&quot;)) {
         result = result.rgbSwapped();
     } else if (filter == tr(&quot;Grayscale&quot;)) {
         for (int y = 0; y &lt; result.height(); ++y) {
             for (int x = 0; x &lt; result.width(); ++x) {
                 int pixel = result.pixel(x, y);
                 int gray = qGray(pixel);
                 int alpha = qAlpha(pixel);
                 result.setPixel(x, y, qRgba(gray, gray, gray, alpha));
             }
         }
     }
     return result;
 }

 Q_EXPORT_PLUGIN2(pnp_basictools, BasicToolsPlugin)</pre>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2008 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt 4.3.5</div></td>
</tr></table></div></address></body>
</html>
